package com.pearadmin.system.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.system.domain.SysTool;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.system.service.ISysToolService;

/**
 * 刀具监测Controller
 * 
 * @author Ryan
 * @date 2021-03-20
 */
@RestController
@RequestMapping("/system/tool")
public class SysToolController extends BaseController
{
    private String prefix = "system/tool";

    @Autowired
    private ISysToolService sysToolService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/system/tool/main','system:tool:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询刀具监测列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/system/tool/data','system:tool:data')")
    public ResultTable list(@ModelAttribute SysTool sysTool, PageDomain pageDomain)
    {
        PageInfo<SysTool> pageInfo = sysToolService.selectSysToolPage(sysTool,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增刀具监测
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/system/tool/add','system:tool:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存刀具监测
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/system/tool/add','system:tool:add')")
    public Result save(@RequestBody SysTool sysTool)
    {
        return decide(sysToolService.insertSysTool(sysTool));
    }

    /**
     * 修改刀具监测
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/system/tool/edit','system:tool:edit')")
    public ModelAndView edit(Long toolId, ModelMap mmap)
    {
        SysTool sysTool = sysToolService.selectSysToolById(toolId);
        mmap.put("sysTool", sysTool);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存刀具监测
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/system/tool/edit','system:tool:edit')")
    public Result update(@RequestBody SysTool sysTool)
    {
        return decide(sysToolService.updateSysTool(sysTool));
    }

    /**
     * 删除刀具监测
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/system/tool/remove','system:tool:remove')")
    public Result batchRemove(String ids)
    {
        return decide(sysToolService.deleteSysToolByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{toolId}")
    @PreAuthorize("hasPermission('/system/tool/remove','system:tool:remove')")
    public Result remove(@PathVariable("toolId") Long toolId)
    {
        return decide(sysToolService.deleteSysToolById(toolId));
    }

    /**
     *根据状态查出对应的条数
     */
    @ResponseBody
    @GetMapping("/selectToolCountByState")
    @PreAuthorize("hasPermission('/system/tool/selectToolCountByState','system:tool:selectToolCountByState')")
    public int selectCountByState(String toolState)
    {
        return sysToolService.selectCountByState(toolState);
    }

}
